package openadmin.model.territory;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import openadmin.dao.Base;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;

@Entity
@Table(name = "municipality", schema = "territory", uniqueConstraints = {@UniqueConstraint(columnNames = "codemunicipalityine"), @UniqueConstraint(columnNames =  {"codemunicipalitydgc", "muniName"}),@UniqueConstraint(columnNames = "description")})
public class Municipality implements Base, java.io.Serializable{
	
	/** attribute that contains the identifier*/
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)	
	private Integer id;

	/** attribute that contains the code municipality INE*/	
	@NotNull
	private Integer codemunicipalityine;
	
	/** attribute that contains the code municipality DGC*/	
	@NotNull
	private Integer codemunicipalitydgc;
		
	/** attribute that contains the municipality name, and is not unique value*/
	@Length(max = 60)
	@NotNull
	private String muniName;
	
	/** attribute that contains the the municipality name and cod province, unique value*/
	@Length(max = 60)
	@NotNull
	private String description;
	
	/** attribute if contains parcels rustic */	
	private Boolean isrustic;
	
	/** attribute if contains parcels urban */	
	private Boolean isurban;
	
	/** Transient attribute that means that the system should make a log on any JPA operation of this class*/
	@Transient
	private boolean debuglog = true;
	
	/** Transient attribute that means that the system should make a historic on any JPA operation of this class*/
	@Transient
	boolean historiclog = false;
	
	/** attribute that contains the relationship with Province*/
	@ManyToOne
	@JoinColumn(name = "province", nullable= false)
	private Province province;
	
	/** attribute that contains the relationship with road*/
	//@OneToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, mappedBy = "municipality")
	//private Set<Street> street = new HashSet<Street>(0);
	
	
	/**
	 * Constructor of the class Municipality.
	 * without parameters
	 */
	public Municipality() {
		
	}
	
	/** Getters and setters*/
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}	

	public String getMuniName() {
		return muniName;
	}

	public void setMuniName(String muniName) {
		this.muniName = muniName.toUpperCase();
	}
	
	public String getDescription() {
		return description;
	}
	
	/* in order to get a unique description */
	public void setDescription() {
		//this.description = muniName.toUpperCase()+"."+Integer.toString(this.province.getCodeProvince());
		//this.description = muniName.toUpperCase()+"."+Integer.toString(this.codemunicipalityine/1000);
		this.description = getProvince().getCodeProvince().toString() + " " + getMuniName();
	}

	public Integer getCodemunicipalityine() {
		return codemunicipalityine;
	}

	public void setCodemunicipalityine(Integer codemunicipalityine) {
		this.codemunicipalityine = codemunicipalityine;
	}

	public Integer getCodemunicipalitydgc() {
		return codemunicipalitydgc;
	}

	public void setCodemunicipalitydgc(Integer codemunicipalitydgc) {
		this.codemunicipalitydgc = codemunicipalitydgc;
	}

	public Boolean getIsrustic() {
		return isrustic;
	}

	public void setIsrustic(Boolean isrustic) {
		this.isrustic = isrustic;
	}

	public Boolean getIsurban() {
		return isurban;
	}

	public void setIsurban(Boolean isurban) {
		this.isurban = isurban;
	}
	
	public boolean getDebugLog() {
		
		return debuglog;
	}
	
	public void setDebugLog(boolean pDebuglog) {
		
		debuglog = pDebuglog;
	
	}

	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province; 
	}	
	
	public boolean getHistoricLog() {
		return historiclog;
	}

	public void setHistoricLog(boolean historiclog) {
		this.historiclog = historiclog;
	}

	@Override
	public String toString(){
		 
		 return description;
	}
	
}
